package com.fc.auth.support.core;

import com.fc.common.core.constant.SecurityConstants;
import com.fc.common.security.service.FcUser;
import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenClaimsContext;
import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenClaimsSet;
import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenCustomizer;

/**
 * @Description: 用户信息增强
 * @ClassName: CustomeOAuth2TokenCustomizer
 * @Author: weisn
 * @Date: 2022/12/19 11:45
 * @Version: 1.0
 */
public class CustomeOAuth2TokenCustomizer implements OAuth2TokenCustomizer<OAuth2TokenClaimsContext> {
    /**
     * @param context
     */
    @Override
    public void customize(OAuth2TokenClaimsContext context) {
        OAuth2TokenClaimsSet.Builder claims = context.getClaims();
        claims.claim(SecurityConstants.DETAILS_LICENSE, SecurityConstants.PROJECT_LICENSE);
        String clientId = context.getAuthorizationGrant().getName();
        claims.claim(SecurityConstants.CLIENT_ID, clientId);
        // 客户端模式不返回具体用户信息
        if (SecurityConstants.CLIENT_CREDENTIALS.equals(context.getAuthorizationGrantType().getValue())) {
            return;
        }

        FcUser fcUser = (FcUser) context.getPrincipal().getPrincipal();
        claims.claim(SecurityConstants.DETAILS_USER, fcUser);
    }
}
